﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.Model;
using KaoQin.BLL;

namespace KaoQin.WPFApp.kaoQin
{
    /// <summary>
    /// EditSwipe.xaml 的交互逻辑
    /// </summary>
    public partial class EditSwipe : Window
    {
        public EditSwipe()
        {
            InitializeComponent();
        }
        public EditSwipe(string ID)
        {
            InitializeComponent();
            bindData(ID);
        }

        private SysWorkTimeParasInfo timeParasInfo;

        private void bindData(string id)
        {
            try
            {
                gInfo.DataContext = new BLL.SwipeCardRecord().GetModel(id);
                timeParasInfo = new KaoQin.BLL.SysWorkTimeParas().GetModel("435B4B55-817D-433E-BD50-1CEAB38DFDC8");
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定数据出错：" + ex.Message);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Model.SwipeCardRecordInfo model = (Model.SwipeCardRecordInfo)gInfo.DataContext;
                
                int state=0;
                string statestring;
                int duan = -1;
                GetSwipeState(out state, out statestring,out duan,model.EmployeeID,model.SwipeCardTime.Value);
                model.KState = state;
                model.duan = duan;
                new BLL.SwipeCardRecord().Update(model);
                this.DialogResult = true;
                MessageBox.Show("修改成功");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改出错:" + ex.Message);
            }
        }

        private bool GetSwipeState(out int KState, out string StateString, out int duan, string ID,DateTime now)
        {
            bool flag = false;
            int state = -1;
            int _duan = -1;
            string strState = string.Empty;
            SwipeCardRecord sbll = new SwipeCardRecord();
            try
            {
                if (timeParasInfo != null)
                {
                    

                    DateTime BeginStart1 = Convert.ToDateTime(timeParasInfo.BeginStart1.Value.ToLongTimeString());
                    DateTime BeginStop1 = Convert.ToDateTime(timeParasInfo.BeginStop1.Value.ToLongTimeString());
                    DateTime BeginTime1 = Convert.ToDateTime(timeParasInfo.BeginTime1.Value.ToLongTimeString());
                    DateTime EndTime1 = Convert.ToDateTime(timeParasInfo.EndTime1.Value.ToLongTimeString());
                    DateTime EndStart1 = Convert.ToDateTime(timeParasInfo.EndStart1.Value.ToLongTimeString());
                    DateTime EndStop1 = Convert.ToDateTime(timeParasInfo.EndStop1.Value.ToLongTimeString());
                    DateTime BeginDelayTime1 = BeginTime1.AddMinutes(timeParasInfo.BeginDelayTime.Value);
                    DateTime EndForwardTime1 = EndTime1.AddMinutes(-timeParasInfo.EndForwardTime.Value);

                    DateTime BeginStart2 = Convert.ToDateTime(timeParasInfo.BeginStart2.Value.ToLongTimeString());
                    DateTime BeginStop2 = Convert.ToDateTime(timeParasInfo.BeginStop2.Value.ToLongTimeString());
                    DateTime BeginTime2 = Convert.ToDateTime(timeParasInfo.BeginTime2.Value.ToLongTimeString());
                    DateTime EndTime2 = Convert.ToDateTime(timeParasInfo.EndTime2.Value.ToLongTimeString());
                    DateTime EndStart2 = Convert.ToDateTime(timeParasInfo.EndStart2.Value.ToLongTimeString());
                    DateTime EndStop2 = Convert.ToDateTime(timeParasInfo.EndStop2.Value.ToLongTimeString());
                    DateTime BeginDelayTime2 = BeginTime2.AddMinutes(timeParasInfo.BeginDelayTime.Value);
                    DateTime EndForwardTime2 = EndTime2.AddMinutes(-timeParasInfo.EndForwardTime.Value);

                    DateTime BeginStart3 = Convert.ToDateTime(timeParasInfo.BeginStart3.Value.ToLongTimeString());
                    DateTime BeginStop3 = Convert.ToDateTime(timeParasInfo.BeginStop3.Value.ToLongTimeString());
                    DateTime BeginTime3 = Convert.ToDateTime(timeParasInfo.BeginTime3.Value.ToLongTimeString());
                    DateTime EndTime3 = Convert.ToDateTime(timeParasInfo.EndTime3.Value.ToLongTimeString());
                    DateTime EndStart3 = Convert.ToDateTime(timeParasInfo.EndStart3.Value.ToLongTimeString());
                    DateTime EndStop3 = Convert.ToDateTime(timeParasInfo.EndStop3.Value.ToLongTimeString());

                    DateTime BeginDelayTime3 = BeginTime3.AddMinutes(timeParasInfo.BeginDelayTime.Value);
                    DateTime EndForwardTime3 = EndTime3.AddMinutes(-timeParasInfo.EndForwardTime.Value);


                    if (now >= BeginStart1 && now <= BeginStop1)
                    {
                        _duan = 11;
                        if (now <= BeginDelayTime1)
                        {
                            state = 0;
                            strState = "正常上班";
                        }
                        else
                        {
                            state = 1;
                            strState = "上班迟到";
                        }
                    }
                    else if (now >= EndStart1 && now <= EndStop1)
                    {
                        _duan = 12;
                        if (now < EndForwardTime1)
                        {
                            if (!sbll.Exists(ID, 11, now))
                            {
                                _duan = 11;
                                state = 1;
                                strState = "上班迟到";
                            }
                            else
                            {

                                state = 3;
                                strState = "下班早退";
                            }

                        }
                        else
                        {
                            state = 2;
                            strState = "正常下班";
                        }
                    }
                    else if (now >= BeginStart2 && now <= BeginStop2)
                    {
                        _duan = 21;
                        if (now <= BeginDelayTime2)
                        {
                            state = 0;
                            strState = "正常上班";
                        }
                        else
                        {
                            state = 1;
                            strState = "上班迟到";
                        }
                    }
                    else if (now >= EndStart2 && now <= EndStop2)
                    {
                        _duan = 22;
                        if (now < EndForwardTime2)
                        {
                            if (!sbll.Exists(ID, 21, now))
                            {
                                _duan = 21;
                                state = 1;
                                strState = "上班迟到";
                            }
                            else
                            {
                                state = 3;
                                strState = "下班早退";
                            }

                        }
                        else
                        {
                            state = 2;
                            strState = "正常下班";
                        }
                    }
                    else if (now >= BeginStart3 && now <= BeginStop3)
                    {
                        _duan = 31;
                        if (now <= BeginDelayTime3)
                        {
                            state = 0;
                            strState = "正常上班";
                        }
                        else
                        {
                            state = 1;
                            strState = "上班迟到";
                        }
                    }
                    else if (now >= EndStart3 && now <= EndStop3)
                    {
                        _duan = 32;
                        if (now < EndForwardTime3)
                        {
                            if (sbll.Exists(ID, 31, now))
                            {
                                _duan = 31;
                                state = 1;
                                strState = "上班迟到";
                            }
                            else
                            {
                                state = 3;
                                strState = "下班早退";
                            }

                        }
                        else
                        {
                            state = 2;
                            strState = "正常下班";
                        }
                    }
                    else
                    {
                        state = 4;
                        strState = "非打卡时间";
                    }






                    flag = true;
                }

                KState = state;
                StateString = strState;
                duan = _duan;

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return flag;

        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }
    }
}
